package com.xy.blog.article.domain.dao.impl;

import com.github.pagehelper.PageHelper;
import com.xy.blog.article.api.model.ArticleCategoryQuery;
import com.xy.blog.article.domain.dao.IArticleCategoryDao;
import com.xy.blog.article.domain.dao.mapper.ArticleCategoryMapper;
import com.xy.blog.article.domain.dao.mapper.ArticleCategoryRelMapper;
import com.xy.blog.article.domain.dao.model.ArticleCategoryDO;
import com.xy.blog.article.domain.dao.model.ArticleCategoryRelDO;
import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.id.SnowflakeIdWorker;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.core.springmvc.result.page.PageResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/5/24
 */
@Repository
public class ArticleCategoryDaoImpl implements IArticleCategoryDao {

    @Autowired
    private ArticleCategoryMapper articleCategoryMapper;

    @Autowired
    private ArticleCategoryRelMapper articleCategoryRelMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ArticleCategoryDO item) throws BaseException {
        if (item.getId() == null) {
            item.setId(SnowflakeIdWorker.generateId());
        }
        if (item.getCreateTime() == null) {
            LocalDateTime now = LocalDateTime.now();
            item.setCreateTime(now);
        }
        if (item.getUpdateTime() == null) {
            LocalDateTime now = LocalDateTime.now();
            item.setUpdateTime(now);
        }
        articleCategoryMapper.insertSelective(item);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(ArticleCategoryDO item) throws BaseException {
        item.setUpdateTime(LocalDateTime.now());
        articleCategoryMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) throws BaseException {
        articleCategoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ArticleCategoryDO getById(Long id) throws BaseException {
        return articleCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResp<ArticleCategoryDO> listByPage(PageReq<ArticleCategoryQuery> req) throws BaseException {
        ArticleCategoryQuery params = req.getParams();
        Example example = new Example(ArticleCategoryDO.class);
        Example.Criteria criteria = example.createCriteria();
        if (params != null && StringUtils.isNoneEmpty(params.getName())) {
            criteria.andLike("name", "%" + params.getName().trim() + "%");
        }
        criteria.andIsNull("parentId");
        PageHelper.startPage(req);
        List<ArticleCategoryDO> articleCategoryDOS = articleCategoryMapper.selectByExample(example);
        return PageResp.data(articleCategoryDOS);
    }

    @Override
    public List<ArticleCategoryDO> select(ArticleCategoryDO item) {
        return articleCategoryMapper.select(item);
    }

    @Override
    public int selectCount(ArticleCategoryDO item) {
        return articleCategoryMapper.selectCount(item);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRel(ArticleCategoryRelDO item) {
        if (item.getId() == null) {
            item.setId(SnowflakeIdWorker.generateId());
        }
        if (item.getCreateTime() == null) {
            LocalDateTime now = LocalDateTime.now();
            item.setCreateTime(now);
        }
        if (item.getUpdateTime() == null) {
            LocalDateTime now = LocalDateTime.now();
            item.setUpdateTime(now);
        }
        articleCategoryRelMapper.insertSelective(item);
    }

    @Override
    public List<ArticleCategoryRelDO> selectRel(ArticleCategoryRelDO item) {
        return articleCategoryRelMapper.select(item);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRelByCategoryId(Long id) {
        ArticleCategoryRelDO articleCategoryRelDO = new ArticleCategoryRelDO();
        articleCategoryRelDO.setCategoryId(id);
        articleCategoryRelMapper.delete(articleCategoryRelDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRelByArticleId(Long id) {
        ArticleCategoryRelDO articleCategoryRelDO = new ArticleCategoryRelDO();
        articleCategoryRelDO.setArticleId(id);
        articleCategoryRelMapper.delete(articleCategoryRelDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRelById(Long id) {
        articleCategoryRelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ArticleCategoryDO> listByLevel(String level) {
        Example example = new Example(ArticleCategoryDO.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("level", level + "/%");
        return articleCategoryMapper.selectByExample(example);
    }

    @Override
    public List<ArticleCategoryDO> listAll() {
        return articleCategoryMapper.selectAll();
    }

    @Override
    public List<ArticleCategoryDO> listParents() {
        Example example = new Example(ArticleCategoryDO.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIsNull("parentId");
        return articleCategoryMapper.selectByExample(example);
    }
}
